package com.vvlive.caibao.smartcity.ui;

import android.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.Event;
import com.vvlive.caibao.smartcity.utils.TimeUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;
import com.vvlive.caibao.smartcity.view.DateTimePickerDialog;

import org.kymjs.kjframe.ui.BindView;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/4.
 */
public class PublishOneEventActivity extends BaseActivity {

    @BindView(id = R.id.poe_theme)
    EditText ETtheme;
    @BindView(id = R.id.poe_choose_time, click = true)
    TextView TVtime;
    @BindView(id = R.id.poe_choose_map, click = true)
    TextView TVmap;
    @BindView(id = R.id.poe_number)
    EditText ETnumber;
    @BindView(id = R.id.poe_content)
    EditText ETcontent;
    @BindView(id = R.id.poe_publish, click = true)
    Button BTNpublish;


    private String theme;
    private String time;
    private String location;
    private int number;
    private String content;
    @Override
    protected int getContentViewRes() {
        return R.layout.activity_publish_one_event;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setTitle("发布活动");
        actionBar.setMoreIcon(R.mipmap.leimu);
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()){
            case R.id.poe_choose_time:
                chooseTime();
                break;
            case R.id.poe_choose_map:
                // 选择地图
                chooseMap();
                break;
            case R.id.poe_publish:
                // 发布活动
                publishEvent();
                break;
        }
    }

    private void chooseMap() {
        toast("选择地图");
    }

    private void chooseTime() {
        DateTimePickerDialog dialog = new DateTimePickerDialog(this, System.currentTimeMillis());
        dialog.setOnDateTimeSetListener(new DateTimePickerDialog.OnDateTimeSetListener() {
            @Override
            public void OnDateTimeSet(AlertDialog dialog, long date) {
                TVtime.setText(TimeUtil.getTargetDateTime(date));
            }
        });
        dialog.show();
    }

    private void publishEvent() {
        theme    = ETtheme.getText().toString();
        time     = TVtime.getText().toString();
        location = TVmap.getText().toString();
        number   = Integer.valueOf(ETnumber.getText().toString());
        content  = ETcontent.getText().toString();
        if( canPublish()){
            alert(new Event(theme, time, location, number, content));
        }else {
            alert("请将发布信息填写完整！");
        }
    }

    private boolean canPublish() {
        return !TextUtils.isEmpty(theme) && !TextUtils.isEmpty(time) && !TextUtils.isEmpty(location)
                && !TextUtils.isEmpty(content) && !TextUtils.isEmpty(ETnumber.getText().toString())? true : false;
    }
}
